var user = {
    "id": -1,
    "username": "",
    "avatar": ""
};

// 登录表单数据对象
var loginForm = {
    "username": "",
    "password": ""
};

// 聊天窗口数据对象
var chatForm = {
    "memberList": [
        {
            "avatar": "static/img/a.jpg",
            "username": "晓明",
            "isAdmin": true
        },
        {
            "avatar": "static/img/a.jpg",
            "username": "大明",
            "isAdmin": true
        },
        {
            "avatar": "static/img/a.jpg",
            "username": "晓红",
            "isAdmin": false
        }
    ],
    "messageList": [
        {
            "username": "晓红",
            "time": "2019-05-28 17:20:33",
            "type": "text",
            "content": "大家好，我是晓红!"
        },
        {
            "username": "晓明",
            "time": "2019-05-28 17:20:53",
            "type": "text",
            "content": "来了，老弟！！！"
        }
    ],
    "content": "",
    "online": 1
};


/** 页面加载完成之后执行 */
$(function () {
    /** 初始化Vue实例 */
    new Vue({
        el: "#app",
        data: {
            user: user,
            loginForm: loginForm,
            chatForm: chatForm
        },
        methods: {
            send: function(){
                // 发送消息的逻辑
                // 需要向后台发起Ajax请求，发送聊天消息
                var message = {
                    "username": user.username,
                    "time": "2019-05-28 17:33:44",
                    "type": "text",
                    "content": chatForm.content
                };

                chatForm.messageList.push(message);
                chatForm.content = "";
            },
            login: function () {
                var pass = false;

                if(loginForm.username.length < 3){
                    layer.msg("用户名格式错误", function (){});
                } else if(loginForm.password < 6){
                    layer.msg("密码格式错误", function (){});
                } else {
                    pass = true;
                }

                if(pass){
                    // 应该发起Ajax登录请求
                    // 这里模拟登录成功，ID=998

                    user.id = 998;
                    user.username = "老樊";
                    user.avatar = "";
                }
            }
        }
    });


});